# 将有序数组转换为二叉搜索树

class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def sortedArrayToBST(self, nums):
        """
        :type nums: List[int]
        :rtype: TreeNode
        """
        return self.build(nums, 0, len(nums)-1)

    def build(self, nums, left, right):
        if left > right:
            return None

        mid = (right + left) // 2

        root = TreeNode(nums[mid])
        root.left = self.build(nums, left, mid-1)
        root.right = self.build(nums, mid+1, right)

        return root
